package com.prb.io;

import java.io.*;
import java.util.Scanner;

public class IoTest {

    public static Integer time = 0;

    public static void main(String[] args) throws IOException {

        //java io study : https://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html

        //createFile();

        //fileSplite();

        //deleteFile();

        //mkdir();

        //dgPrint();

        //randonAccessFile();


        //字节流是直接操作文件本身  ， 字符流 是操作缓冲区
        //fileOs();
        //fileReader();


        //writeByte();

        //fileIss();

        //fileIs();
        //fileWriter();


        //bufReader();

        //kbInput();

        //scInput();


    }

    private static void scInput() {
        Scanner sc = new Scanner(System.in);

        int i = sc.nextInt();
        System.out.println(i);

        String r = sc.next();
        System.out.println(r);
    }

    private static void kbInput() throws IOException {
        BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("input content");
        String r = bfr.readLine();

        System.out.println("input content is : " + r);
    }

    private static void bufReader() throws IOException {
        File file = new File("prb.txt");
        FileReader fr = new FileReader(file);

        BufferedReader bfr = new BufferedReader(fr);

        char[] cbuf = new char[1024];
        bfr.read(cbuf);
        System.out.println(new String(cbuf));

        String s = bfr.readLine();
        System.out.println(s);
        String s1 = bfr.readLine();
        System.out.println(s1);
    }

    private static void fileIss() throws IOException {
        File file = new File("prb.txt");

        FileInputStream fis = new FileInputStream(file);

        byte[] bytes = new byte[1024];

        int read = fis.read(bytes);

        System.out.println(new String(bytes));

        System.out.println("file len is: " + read);
        System.out.println("file length is : " + file.length());
    }

    private static void writeByte() throws IOException {
        File file = new File("prb.txt");

        FileOutputStream fos = new FileOutputStream(file , true);

        String str = "116,101,115,116";
        String[] split = str.split(",");

        for (int i = 0; i < split.length; i++) {
            fos.write(Byte.parseByte(split[i]));
        }
    }

    private static void randonAccessFile() throws IOException {
        File file = new File("prb.txt");

        RandomAccessFile rf = new RandomAccessFile(file , "rw");

        rf.writeInt(65);
    }

    private static void dgPrint() {
        File file = new File("f:" + File.separator + "有道云");
        printFile(file , 0);
    }


    private static void printFile(File file , Integer deep) {

        for (int i = 0; i < deep; i++) {
            System.out.print("\t");
        }


        if(file.exists()){
            if(file.isDirectory()){
                System.out.println(file.getName());
                File[] list = file.listFiles();
                for (File s : list) {
                    printFile(s , deep + 1);
                }
            }else{
                System.out.println(file.getName());

            }
        }else{
            System.out.println("目录不存在");
        }

    }

    private static void mkdir() {
        File file = new File("pdir");
        if(file.exists()){
            System.out.println("目录已经存在");
        }else{
            System.out.println("目录不存在，进行创建");
            file.mkdirs();
        }
    }

    private static void deleteFile() {
        File file = new File("prb.txt");
        if(file.exists()){
            System.out.println("文件已经存在，进行删除");
            file.delete();
        }else{
            System.out.println("文件不存在，不需要删除");
        }
    }

    private static void fileSplite() {
        System.out.println("文件切分符号" + File.separator);  //文件切分符号\
        System.out.println("文件路径切分符号" + File.pathSeparatorChar); //文件路径切分符号;
    }

    private static void createFile() throws IOException {
        File file = new File("prb.txt");
        if(!file.exists()){
            System.out.println("file is no exist");
            file.createNewFile();
        }else{
            System.out.println("file is exist");
        }
    }

    private static void fileWriter() throws IOException {
        FileWriter writer = new FileWriter("writer.txt" , true);
        writer.append("你好啊\n");
        writer.append("谢谢你");
        writer.append("再见");
        //writer.flush();
        //writer.close();
    }

    private static void fileReader() throws IOException {
        FileReader reader = new FileReader("test.txt");

        char[] cbuf = new char[1024 * 1024];
        while (reader.read(cbuf) != -1){
            time++;
            System.out.println(cbuf);

        }
    }

    private static void fileIs() throws IOException {
        InputStream is = new FileInputStream("test.txt");

        byte[] buffer = new byte[1024];
        while (is.read(buffer) != -1){
            System.out.println(new String(buffer));
        }
    }

    private static void fileOs() throws IOException {
        OutputStream os = new FileOutputStream("test.txt" , true);

        os.write("hello \n".getBytes());
        os.write("ni hao a ".getBytes());

        //os.flush();

        //os.close();
    }


}
